import path from 'path';

import fs from 'fs-extra';

import { buildAppAndHapsAndCheckFile } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.NativeC,
  },
  modulesOption: {
    [ModuleTemplateType.NativeC]: 1,
  },
};
const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

// eslint-disable-next-line max-lines-per-function
const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const entryPath = project.getModule('entry').moduleRootPath();
  const nativeAppPath = project.getModule(ModuleTemplateType.NativeC).moduleRootPath();
  const insightIntentJsonPath = path.resolve('test', 'resources', 'insightintents', 'hasMusicNMscList', 'insight_intent.json');

  // 两个模块都全部勾选
  fs.cpSync(
    path.resolve('test', 'resources', 'insightintents', 'hasMusicNMscList', 'IntentExecutorImpl.ets'),
    path.resolve(entryPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets'),
  );
  fs.cpSync(insightIntentJsonPath, path.resolve(entryPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json'));
  fs.cpSync(
    path.resolve('test', 'resources', 'insightintents', 'hasMusicNMscList', 'IntentExecutorImpl.ets'),
    path.resolve(nativeAppPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets'),
  );
  const insightIntentJsonFile = fs.readFileSync(insightIntentJsonPath);
  const ability2InsightIntentJson = JSON.parse(insightIntentJsonFile.toString());
  ability2InsightIntentJson.insightIntents.forEach((item: Record<string, Record<string, string>>) => {
    item.uiAbility.ability = 'ApplicationAbility';
  });

  fs.writeFileSync(
    path.resolve(nativeAppPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json'),
    JSON.stringify(ability2InsightIntentJson),
  );

  const entryInsightintentsPath = path.resolve(entryPath, 'build', 'default', 'cache', 'default', 'default@CompileArkTS', 'esmodule', 'debug', 'entry',
    'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.protoBin',);
  const entryInsightintentJsonPath = path.resolve(entryPath, 'build', 'default', 'intermediates', 'res', 'default', 'resources', 'base', 'profile',
    'insight_intent.json',);
  const nativeAppInsightintentsPath = path.resolve(nativeAppPath, 'build', 'default', 'cache', 'default', 'default@CompileArkTS', 'esmodule', 'debug',
    ModuleTemplateType.NativeC, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.protoBin',);
  const nativeAppInsightintentJsonPath = path.resolve(nativeAppPath,'build','default','intermediates','res','default','resources','base','profile',
    'insight_intent.json',);
  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
  expect(await fs.pathExists(entryInsightintentsPath)).toBe(true);
  expect(await fs.pathExists(entryInsightintentJsonPath)).toBe(true);
  expect(await fs.pathExists(nativeAppInsightintentsPath)).toBe(true);
  expect(await fs.pathExists(nativeAppInsightintentJsonPath)).toBe(true);
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_ZOS_BUILD_HAP_INTENT_FRAMEWORK_003')
  .description('003、native工程添加意图框架模块构建')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
